package com.muhuahealth.utils;

import java.io.Serializable;

/**
 * 通用响应对象，ajax请求返回的数据都封装成ServerResponse
 * 类比：PageBean
 * @author GSK
 * @version 1.0
 * @date 2021/1/1 19:17
 */

public class ServerResponse<T> implements Serializable {
    private static final int SUCCESS = 0;
    private static final int ERROR = 1;
    private static final int NEED_LOGIN = 2;
    private static final int ILLEGAL_PERMISSION = 3;

    //当前状态（程序员判断状态）:成功、失败、未登录、没有权限
    private Integer codes;
    //描述信息（主要是给用户看的提示信息）
    private String msg;
    //总数量（按理说是不需要，主要是给LayUI table）
    private Integer count;
    //后台返回给前端的数据 Object
    private T data; // List<Student>, int, String, true

    private ServerResponse() {
    }

    private ServerResponse(Integer codes) {
        this.codes = codes;
    }

    private ServerResponse(Integer codes, String msg) {
        this.codes = codes;
        this.msg = msg;
    }

    private ServerResponse(Integer codes, String msg, T data) {
        this.codes = codes;
        this.msg = msg;
        this.data = data;
    }

    private ServerResponse(Integer codes, String msg, Integer count, T data) {
        this.codes = codes;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }

    //只是告诉前台：成功这种状态
    public static<T> ServerResponse<T> createSuccess() {
        return new ServerResponse<>(SUCCESS);
    }

    //告诉前台：code,msg
    public static <T> ServerResponse<T> createSuccess(String msg) {
        return new ServerResponse<>(SUCCESS, msg);
    }

    //告诉前台：code,msg,data
    public static <T> ServerResponse<T> createSuccess(String msg, T data) {
        return new ServerResponse<>(SUCCESS, msg, data);
    }

    //告诉前台：code,msg,count,data
    public static <T> ServerResponse<T> createSuccess(String msg, Integer count, T data) {
        return new ServerResponse<>(SUCCESS, msg, count, data);
    }

    //只是告诉前台：失败这种状态
    public static <T> ServerResponse<T> createError() {
        return new ServerResponse<>(ERROR);
    }

    //告诉前台：code,msg
    public static <T> ServerResponse<T> createError(String msg) {
        return new ServerResponse<>(ERROR, msg);
    }

    //告诉前台：code,msg,data
    public static <T> ServerResponse<T> createError(String msg, T data) {
        return new ServerResponse<>(ERROR, msg, data);
    }

//	public boolean isSuccess() {
//		return code == SUCCESS;
//	}

    public Integer getCode() {
        return codes;
    }

    public void setCode(Integer code) {
        this.codes = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public static void main(String[] args) {
        ServerResponse serverResponse2 = new ServerResponse();
        serverResponse2.setCode(SUCCESS);
        serverResponse2.setMsg("成功");

        ServerResponse serverResponse1 = ServerResponse.createSuccess();
        ServerResponse server = ServerResponse.createSuccess("删除成功");
    }


}

